//
//  UserInfoView.m
//  Weibo
//
//  Created by taoshanchang on 15/2/2.
//  Copyright (c) 2015年 taoshanchang. All rights reserved.
//

#import "UserInfoView.h"
#import "UIImageView+WebCache.h"
#import "UIViewExt.h"
@implementation UserInfoView

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if(self){
        [self _initView];
    }
    return self;
}

-(void)_initView{
    //用户头像
    _userImage = [[UIImageView alloc] initWithFrame:CGRectZero];
    _userImage.image = [UIImage imageNamed:@"page_image_loading"];
    //用户昵称
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _nameLabel.font = [UIFont systemFontOfSize:16.0f];
    _nameLabel.backgroundColor = [UIColor clearColor];
    //用户地址
    _addressLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _addressLabel.font = [UIFont systemFontOfSize:16.0f];
    _addressLabel.backgroundColor = [UIColor clearColor];
    //用户信息
    _infoLabel = [[UILabel alloc ] initWithFrame:CGRectZero];
    _infoLabel.font = [UIFont systemFontOfSize:16.0f];
    _infoLabel.backgroundColor = [UIColor clearColor];
    //关注数
    _countLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _countLabel.font = [UIFont systemFontOfSize:16.0f];
    _countLabel.backgroundColor = [UIColor clearColor];
    //关注按钮
    _attButton = [[RectButton alloc] initWithFrame:CGRectZero];
    //
    _fanButton = [[RectButton alloc] initWithFrame:CGRectZero];
    
    [self addSubview:_userImage];
    [self addSubview:_nameLabel];
    [self addSubview:_addressLabel];
    [self addSubview:_infoLabel];
    [self addSubview:_countLabel];
    [self addSubview:_attButton];
    [self addSubview:_fanButton];

}

-(void)layoutSubviews{
    [super layoutSubviews];
    NSString *userImageUrl = _user.avatar_large;
    NSString *gender = _user.gender;
    NSString *address = _user.location;
    NSString *count = [_user.statuses_count stringValue];
    
    long favL = [self.user.favourites_count longValue];
    NSString *fans = [NSString stringWithFormat:@"%ld",favL];
    if(favL > 9999){
        fans = [NSString stringWithFormat:@"@ld万",favL/10000];
    }
    
    if(address == nil){
        address = @"";
    }
    NSString *sexName = @"未知";
    if ([gender isEqualToString:@"f"]) {
        sexName = @"女";
    }else if([gender isEqualToString:@"m"]){
        sexName = @"男";
    }
    
    
    _userImage.frame = CGRectMake(5, 5, 80, 80);
    [_userImage sd_setImageWithURL:[NSURL URLWithString:userImageUrl]];
    
    _nameLabel.frame = CGRectMake(_userImage.right+10, 5, 100, 30);
    _nameLabel.text = _user.screen_name;
    
    _addressLabel.frame = CGRectMake(_userImage.right+10, _nameLabel.bottom , 100, 30);
    _addressLabel.text = [NSString stringWithFormat:@"%@   %@",sexName,address];
    
    _infoLabel.frame = CGRectMake(_userImage.right+10, _addressLabel.bottom , 100, 30);
    _infoLabel.text = (_user.description==nil)?@"这个用户什么都没有留下":_user.description;
    
    _countLabel.text = [NSString stringWithFormat:@"共%@条微博",count];
    
    _fanButton.title = @"粉丝";
    _fanButton.subTitle = fans;
    
    _attButton.title = @"关注";
    _attButton.subTitle = [self.user.friends_count stringValue];
}

@end
